﻿using BoriPetr.OAuth;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BoriPeter.FatSecret.Messages
{
    public class RequestToken : OAuthMessageBase<OAuthToken>
    {
        private const string NAME_CALLBACK = "oauth_callback";
        private const string OAUTH_CALLBACK = "oob";
        private const string OAUTH_TOKEN_SECRET = "oauth_token_secret";

        public RequestToken(FatSecretSettings _settings) : base(_settings, new OAuthToken())
        {
            urlBase = _settings.RequestTokenUrlBase;
            AddRequestParameter(NAME_CALLBACK, OAUTH_CALLBACK);
        }

        protected override OAuthToken ProcessResponse(string response)
        {
            var parameters = new Dictionary<string, string>();

            String[] keyValPairs = response.Split('&');

            for (int i = 0; i < keyValPairs.Length; i++)
            {
                String[] splits = keyValPairs[i].Split('=');
                parameters.Add(splits[0], splits[1]);
            }
            OAuthToken token = new OAuthToken(parameters[OAUTH_TOKEN], parameters[OAUTH_TOKEN_SECRET]);
            Debug.WriteLine("Request TOKEN: {0} SECRET: {1}", parameters[OAUTH_TOKEN], parameters[OAUTH_TOKEN_SECRET]);
            return token;
        }
    }
}
